package com.sx.art2.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.sx.art2.beans.Art;
import com.sx.art2.beans.AuctionInfo;
import com.sx.art2.beans.Stock;
import com.sx.art2.service.ArtService;

@Controller
@RequestMapping("/Art")
public class ArtController { 
	
	@Autowired
	ArtService artService;
	
	
	
	@RequestMapping("/insert")
	public ModelAndView insert(Art art, Stock stock) {
		
		ModelAndView mv=new ModelAndView();
		artService.insert(art);
		mv.setViewName("admin/Products_List");
		return mv;
	}
	@RequestMapping("/list")
	public ModelAndView list() {
		
		ModelAndView mv=new ModelAndView();
		List<Art> artList=artService.list();
		mv.addObject("artList", artList);
		mv.setViewName("admin/Products_List");
		return mv;
	}
	@RequestMapping("/update")
	public ModelAndView update(int artId) {
		
		ModelAndView mv=new ModelAndView();
		Art art=artService.getArt(artId);
		mv.addObject("art", art);
		mv.setViewName("admin/art-update");
		return mv;
	}
	@RequestMapping("/getArt")
	public ModelAndView getArt(int artId,int sign) {
		
		ModelAndView mv=new ModelAndView();
		Art art=artService.getArt(artId);
		mv.addObject("art", art);
		
		
		if(sign==1)
			mv.setViewName("admin/art-show");
		else if(sign==2)
		   mv.setViewName("admin/shangjia");
		return mv;
	}
	@RequestMapping("/artPutAway")
	public ModelAndView artPutAway(AuctionInfo auctionInfo,HttpServletRequest request) throws IllegalStateException, IOException{
		ModelAndView mv=new ModelAndView();
		String pic=null;
		CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
               request.getSession().getServletContext());
       if(multipartResolver.isMultipart(request))
       {
           //将request变成多部分request
           MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
               MultipartFile file=multiRequest.getFile("file");
               if(file!=null)
               {System.out.println(125);
            	   String filename=file.getOriginalFilename();
            	   String exname=filename.substring(filename.lastIndexOf("."));
					String newname=UUID.randomUUID().toString()+exname;
					//String path=request.getSession().getServletContext().getRealPath("images")+"/images/"+file.getOriginalFilename();
                   String path="C:\\Users\\dell\\work_of_work_of_art\\Art2.0\\WebContent\\images"+"/"+newname;
					//上传
                   file.transferTo(new File(path));
                   pic="/images/"+newname;
                   System.out.println(newname.length());
               }
       }
       else
		 System.out.println("out");          
      artService.addAuction(pic,auctionInfo);
		mv.setViewName("redirect:/Art/list");
      return mv;
	}
	@RequestMapping("/lookArt")
	public ModelAndView lookArt(int artId) {
		ModelAndView mv=new ModelAndView();
		Art art=artService.lookArt(artId);
		mv.addObject("art", art);
		System.out.println(art);
		mv.setViewName("pro_detail");
		return mv;
	}
	@RequestMapping("/selectAllArt")
	public ModelAndView selectAllArt(Art art) {
		ModelAndView mv=new ModelAndView();
		List<Art> arts=artService.selectAllArt(art);
		mv.addObject("arts", arts);
		System.out.println(arts);
		mv.setViewName("mall2");
		return mv;
	}


}
